﻿#region

using System.Linq;
using CuongHanh.Context;
using CuongHanh.DAL;
using CuongHanh.Models;

#endregion

namespace CuongHanh.Service
{
    public class MyStoneService : BaseService
    {
        public static string Gets(int start,
                int limit,
                string q)
        {
            var query = CacheContext<MyStone>.Singleton.GetObjectsByFilter(c => (c.Code.Contains(q) || c.Name.Contains(q)) && !c.Inactive)
                    .Select(c => new
                                 {
                                         c.Id,
                                         c.Name,
                                         c.Code,
                                         c.Image,
                                         c.LastModified
                                 });

            return FormatResult(query.Count(),
                    query.OrderByDescending(c => c.LastModified).Skip(start).Take(limit));
        }

        public static MyStone Get(int id)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                return unitOfWork.GetById<MyStone>(id);
            }
        }

        public static UpdateResult Insert(MyStone model)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                unitOfWork.Insert(model);
                unitOfWork.Save();
                CacheContext<MyStone>.Singleton.UpdateCached(model);
                return new UpdateResult(1,
                        string.Format("Insert MyStone {0} successful!",
                                model.Name));
            }
        }

        public static UpdateResult Update(MyStone newModel)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                MyStone model = unitOfWork.GetById<MyStone>(newModel.Id);
                if(model != null)
                {
                    model.UpdateInfo(newModel);
                    unitOfWork.Save();
                    CacheContext<MyStone>.Singleton.UpdateCached(model);
                    return new UpdateResult(1,
                            string.Format("Update MyStone Id = {0} successful!",
                                    newModel.Id));
                }
                return new UpdateResult(4,
                        string.Format("Update MyStone Id = {0} error!",
                                newModel.Id));
            }
        }

        public static UpdateResult Delete(int id)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                MyStone oldModel = unitOfWork.GetById<MyStone>(id);
                if(oldModel != null)
                {
                    oldModel.Delete();
                    unitOfWork.Save();
                    CacheContext<MyStone>.Singleton.UpdateCached(oldModel);
                    return new UpdateResult(1,
                            string.Format("Delete MyStone Id = {0} successful!",
                                    id));
                }
                return new UpdateResult(4,
                        string.Format("Delete MyStone Id = {0} error!",
                                id));
            }
        }

        public static string GetsCombo()
        {
            var query = CacheContext<MyStone>.Singleton.GetObjectsByFilter(c => !c.Inactive)
                    .Select(c => new
                                 {
                                         c.Id,
                                         Name = c.Code + "=>" + c.Name
                                 }).OrderBy(c => c.Name).ToList();

            return FormatResult(query.Count,
                    query);
        }

        public static string GetsCombo(int styleId, int? stoneId)
        {
            var stoneIds = CacheList<StyleStone>.Singleton.GetCache(styleId).Select(c => c.StoneId).ToList();
            if (stoneId.HasValue)
                stoneIds.Remove(stoneId.Value);

            var query = CacheContext<MyStone>.Singleton.GetObjectsByFilter(c => !stoneIds.Contains(c.Id) && !c.Inactive)
                  .Select(c => new
                  {
                      c.Id,
                      c.Name
                  }).OrderBy(c => c.Name).ToList();

            return FormatResult(query.Count,
                    query);
        }
    }
}
